var express = require("express");
var router = express.Router();
var pomelo = require("pomelo");
var logger = require('pomelo-logger').getLogger(__filename);

//validate signature
router.get("/send", function(req, res) {
    var msg = req.query;
    pomelo.app.rpc.account.mailRemote.sendMail(null, msg, function(data) {
        res.set({
            'content-type': 'application/json; charset=utf-8'
        }).send(JSON.stringify(data));
    });
});

router.post("/send", function(req, res) {
    var postData = '';
    req.setEncoding("utf8");
    req.on('data', function (chunk) {
        postData += chunk;
    });
    req.on('end', function () {
        postData = JSON.parse(postData);
        logger.info("send mail post data:", postData);
        pomelo.app.rpc.account.mailRemote.sendMail(null, postData, function(data) {
            logger.info("send mail callback data:", JSON.stringify(data));
            res.write(JSON.stringify(data));
            res.end();
        });
    });
});


module.exports = router;